import 'package:changxindianzishangwu/animation/voice_animation_image.dart';
import 'package:changxindianzishangwu/dao/collect_dao.dart';
import 'package:changxindianzishangwu/model/collect_list_model.dart';
import 'package:changxindianzishangwu/model/dynamic_media_model.dart';
import 'package:changxindianzishangwu/util/app_color.dart';
import 'package:changxindianzishangwu/util/colors.dart';
import 'package:changxindianzishangwu/util/head_url.dart';
import 'package:changxindianzishangwu/util/show_custom_bottom_sheet.dart';
import 'package:changxindianzishangwu/util/toast.dart';
import 'package:changxindianzishangwu/widget/app_bar.dart';
import 'package:flutter/material.dart';
import 'package:flutter_plugin_record/flutter_plugin_record.dart';
import 'package:get/get.dart';

import 'collect_note_page.dart';

///收藏详情页

class CollectDetailsPage extends StatefulWidget {
  CollectBean bean;

  CollectDetailsPage(this.bean, {Key key}) : super(key: key);

  @override
  _CollectDetailsPageState createState() => _CollectDetailsPageState();
}

class _CollectDetailsPageState extends State<CollectDetailsPage> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  BuildContext _context;
  String _remarks = "";

  @override
  Widget build(BuildContext context) {
    _context = context;
    return WillPopScope(
      onWillPop: (){
        Get.back(result: _remarks);
      },
      child: Scaffold(
        appBar: TopAppBar(
          context,
          "详情",
          rightWidget: Icon(Icons.more_horiz),
          rightCallback: () {
            _selectAction();
          },
          leftCallBack: () {
            Get.back(result: _remarks);
          },
        ),
        body: Padding(
          padding: const EdgeInsets.all(15),
          child: Container(
            padding: EdgeInsets.all(10),
            width: MediaQuery.of(context).size.width,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(6),
            ),
            child: _content(),
          ),
        ),
      ),
    );
  }

  _content() {
    switch (widget.bean.type) {
      case 5: //文本
        return _text();
      case 1:
        return _image();
    }
  }

  Text _text() {
    return Text(
      widget.bean.msg,
      style: TextStyle(color: AppColor.colorDefaultText, fontSize: sp(16)),
    );
  }

  Image _image() {
    return Image.network(
      widget.bean.msg,
      width: MediaQuery.of(_context).size.width,
      fit: BoxFit.cover,
    );
  }

  ///更多操作弹窗 dialog
  void _selectAction() async {
    int index = await showDialog(context: context, title: '更多操作');
    // if (index == 1) {}
    if (index == 2) {
      ///备注
      _remarks = await Get.to(CollectNotePage(widget.bean));
    }
    if (index == 3) {
      ///删除
      _deleteCollect();
    }
  }

  ///删除选中收藏
  void _deleteCollect() async {
    var value = await CollectDao.collectDelete(widget.bean.emojiId);
    if (value == 0) {
      toast("删除失败");
      return;
    }
    if (value == 1) {
      toast("删除成功");
      Get.back(result: true);
    }
  }

  Future<int> showDialog({
    @required BuildContext context,
    @required String title,
  }) {
    return showCustomBottomSheet(
        context: context,
        title: title,
        cancelTextColor: AppColor.colorDialog,
        children: [
          // actionItem(
          //     context: context,
          //     index: 1,
          //     title: '发送给朋友',
          //     color: AppColor.colorDialog),
          actionItem(
              context: context,
              index: 2,
              title: '添加备注',
              color: AppColor.colorDialog),
          actionItem(
              context: context,
              index: 3,
              title: '删除',
              isLastOne: true,
              color: Colors.red),
        ]);
  }
}
